<?php
namespace App\Services;

use App\Models\MemberModel;
use App\Models\PayLogModel;
use App\Models\UserModel;
use Carbon\Carbon;
use Framework\BaseService;
use Illuminate\Support\Facades\Log;
use Kra8\Snowflake\Snowflake;

class PayService extends BaseService
{

    public function pay($user_id, $member_id,$type)
    {
        $info = UserModel::findOrFail($user_id);
        if (!$info) {
            $this->retError('用户不存在！');
        }

        $member_info = MemberModel::query()->where('id',$member_id)->first();
        if(!$member_info){
            $this->retError('非法请求！');
        }

        if(!in_array($type,['WeChatPay','AliPay'])){
            $this->retError('非法请求！');
        }

        $money =  $member_info->now_price;
        $msg = "购买".$member_info->name;

        $snowflake = app(Snowflake::class);
        $uuid = $snowflake->next();

        $pay_log = new PayLogModel();
        $pay_log->user_id = $user_id;
        $pay_log->member_id = $member_info->id;
        $pay_log->pay_code = $uuid;
        $pay_log->pay_amount = $money;
        $pay_log->saveOrFail();


        if($type === 'WeChatPay'){
            $response = app('wechat_pay')->app([
                'out_trade_no' => $uuid,
                'body' => $msg,
                'total_fee' => $money * 100,
            ]);
            Log::channel('paylog')->info('wechatAppPay_'.$uuid);
            return $response->send();
        }
        if($type === 'AliPay'){
            $response = app('alipay')->app([
                'out_trade_no' => strval($uuid),
                'total_amount' => bcadd($money, 0, 2),
                'subject' => $msg,
            ]);
            Log::channel('paylog')->info('aliAppPay_'.$uuid);
            return $response->send();
        }

    }

    /**  微信回调
     * @return string
     */
    public function wechatNotify()
    {

        // 校验回调参数是否正确
        $data = app('wechat_pay')->verify();

        //Log::channel('paylog')->info('aliAppPay_微信到这里了2'.$data);


        $Order = PayLogModel::query()->where('pay_code',$data->out_trade_no)->first();
        if (!$Order) {
            return 'fail';
        }

        $Order->pay_type = 1;
        $Order->status = 2;
        $Order->pay_sn = $data->transaction_id;
        $Order->saveOrFail();

        $member_id = MemberModel::query()->where('id',$Order->member_id)->value('time');
        $execute_time = Carbon::now()->addDays($member_id);

        $user = UserModel::query()->where('id',$Order->user_id)->first();
        $user->vip_lv = $Order->member_id;
        $user->vip_expires_time = $execute_time;

        return app('wechat_pay')->success();

    }

    /** 支付宝回调
     * @return string
     */
    public function alipayNotify()
    {
        $data = app('alipay')->verify();

        if (!in_array($data->trade_status, ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
            return app('alipay')->success();
        }
        $Order = PayLogModel::query()->where('pay_code',$data->out_biz_no)->first();
        if (!$Order) {
            return 'fail';
        }


        $Order->pay_type = 2;
        $Order->status = 2;
        $Order->pay_sn = $data->trade_no;
        $Order->saveOrFail();

        $member_id = MemberModel::query()->where('id',$Order->member_id)->value('time');
        $execute_time = Carbon::now()->addDays($member_id);

        $user = UserModel::query()->where('id',$Order->user_id)->first();
        $user->vip_lv = $Order->member_id;
        $user->vip_expires_time = $execute_time;

        return app('alipay')->success();

    }



}
